﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace botliga
{
    public class GameStart:IEnumerable
    {
        List<string> Players;
        // excluder excluded
        List<Tuple<string, string>> Exclude;

        public List<string> PotentialExclude
        {
            get {
                HashSet<string> temp = new HashSet<string>();
                foreach (var x in Exclude)
                {
                    temp.Add(x.Item2);
                }
                return temp.ToList();
            }
        }
        List<string> Excluded;
        List<Tuple<string, string>> Traunt;

        public List<string> PotentialTruant
        {
            get
            {
                HashSet<string> temp = new HashSet<string>();
                foreach (var x in Traunt)
                {
                    temp.Add(x.Item2);
                }
                return temp.ToList();
            }
        }
        List<string> Traunted;

        public List<string> _Traunted
        {
            get { return Traunted; }
            set { Traunted = value; }
        }
        public List<double> Diffs
        {
            get;
            set;
        }
        public List<int> Teams
        {
            get;
            set;
        }
        public List<int> Streak
        {
            get;
            set;
        }
        public List<string> _Excluded
        {
            get { return Excluded; }
            set { Excluded = value; }
        }
        public List<string> _Players
        {
            get { return Players; }
            set { Players = value; }
        }
        List<string> Result;

        public List<string> Results
        {
            get { return Result; }
            
        }
        DateTime Time;
        Game game;
        resultado result;

        internal resultado ResultadoParcial
        {
            get { return result; }
        }
        int resultadoCont;
        public GameStart()
        {
            Result = new List<string>();
            game = new Game(DateTime.Now);
            Players = new List<string>(10);
            Time = DateTime.UtcNow;
            resultadoCont = 0;
            result = resultado.none;
            Exclude = new List<Tuple<string, string>>();
            Excluded = new List<string>();
            Traunt = new List<Tuple<string, string>>();
            Traunted = new List<string>();
            Teams = new List<int>();
            Streak = new List<int>();
            Diffs = new List<double>();
            result = resultado.none;
        }
        public void AddPlayer(String nombre)
        {
            Players.Add(nombre);
        }
        public bool DeletePlayer(String nombre)
        {
            if (Players.Contains(nombre))
            {
                Players.Remove(nombre);
                return true;
            }
            else
                return false;
        }
        public void AddTime()
        {
            Time = DateTime.UtcNow;
        }
        public DateTime GetTime()
        {
            return Time;
        }

        public bool Contains(string User)
        {
            return Players.Contains(User);
        }
        public int Count()
        {
            return Players.Count;
        }


        public IEnumerator GetEnumerator()
        {
            return Players.GetEnumerator();
        }

        public void Start(int Gid)
        {
            game.Fecha = DateTime.Now;
            //String aux = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + Gid.ToString();
            
            //game.idGame = int.Parse(aux);
        }
        public Game GetGame()
        {
            return this.game;
        }
        

        public int GetResults()
        {
           return Result.Count;
        }

        public Tuple<bool,bool> AddResult(resultado __resultado, string User)
        {
            bool @return = false ;
            if (resultado.none == this.result)
            {
                this.result = __resultado;
                @return= true;
            }
            bool @return2;
            if (this.result == __resultado)
            {

                Result.Add(User);
                resultadoCont++;
                @return2 = true;
            }
            else
            {
                @return2 = false;
            }
            
            return new Tuple<bool,bool>(@return,@return2);
        }

        public bool ReportedResult(string User)
        {
            return Result.Contains(User) ;
        }

        internal bool ExcludeVote(string excluder, string excluded)
        {
            bool x;
            if (CountExclude(excluded) == 0)
            {
               
                x = true;
            }
            else
            {
                x = false;
            }
            Exclude.Add(new Tuple<string, string>(excluder, excluded));
            return x;
        }

        internal int CountExclude(string excluded)
        {
            return Exclude.Count(x => x.Item2 == excluded);
        }

        internal void ExcludePlayer(string excluded)
        {
            Excluded.Add(excluded);
        }

        internal bool ExcludeVoted(string excluder,string Payload)
        {
            return Exclude.Contains(new Tuple<string, string>(excluder, Payload));
        }

        internal bool TrauntVoted(string traunter,string Payload)
        {
            return Traunt.Contains(new Tuple<string, string>(traunter, Payload));
        }

        internal bool TrauntVote(string traunter, string traunted)
        {
            bool x;
            if (CountTraunt(traunted) == 0)
            {

                x = true;
            }
            else
            {
                x = false;
            }
            Traunt.Add(new Tuple<string, string>(traunter, traunted));
            return x;
        }

        internal int CountTraunt(string traunted)
        {
            return Traunt.Count(x => x.Item2 == traunted);
        }

        internal void TrauntPlayer(string traunted)
        {
            Traunted.Add(traunted);
        }

        internal List<string> getExcluders(string x)
        {
            return (from ex in Exclude
                    where ex.Item2 == x
                    select ex.Item1).ToList();
        }

        internal List<string> getTraunters(string traunted)
        {
            return (from ex in Traunt
                    where ex.Item2 == traunted
                    select ex.Item1).ToList();
        }

        internal void SetResult()
        {
            game._resultado = result;
        }

        internal bool havePoints()
        {
            if (Diffs.Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

    }
    public class FirstTupleComparer : IEqualityComparer<Tuple<string, string>>
    {

        public bool Equals(Tuple<string, string> b1, Tuple<string, string> b2)
        {
            if (b1.Item1 == b2.Item1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public int GetHashCode(Tuple<string, string> bx)
        {
            return bx.GetHashCode();
        }

    }

    public class GameComparerID : IEqualityComparer<GameStart>
    {
        public bool Equals(GameStart x, GameStart y)
        {
            return x.GetGame().idGame == y.GetGame().idGame;
        }

        public int GetHashCode(GameStart obj)
        {
            return obj.GetGame().idGame.GetHashCode();
        }
    }
}
